<template>
  <view class="login-bg">
    <!-- 模拟登录 -->
    <!-- <view class="">
      <uni-forms :modelValue="formData">
        <uni-forms-item label="token" name="name">
          <input type="text" v-model="formData.token" placeholder="token" />
        </uni-forms-item>
        <uni-forms-item label="userid" name="age">
          <input type="text" v-model="formData.userid" placeholder="userid" />
        </uni-forms-item>
      </uni-forms>
      <button @click="submitForm">模拟登录</button>
    </view> -->
    <!-- <form @submit="" @reset=""> -->
    <view class="container">
      <image class="logoImg" src="/static/images/ic_logo.png"></image>
      <!-- #ifdef  MP-ALIPAY  -->
      <button
        class="submit_btn"
        type="primary"
        open-type="getAuthorize"
        scope="userInfo"
        @getAuthorize="getOpenUserInfo"
      >
        支付宝一键登录
      </button>

      <!-- #endif -->
      <!-- #ifdef  MP-WEIXIN  -->
      <button
        class="submit_btn"
        type="primary"
        open-type="getUserInfo"
        @getuserinfo="getUserInfo"
      >
        微信一键登录
      </button>
      <!-- #endif -->
    </view>
    <!-- </form> -->
    <companyInfo :companyNames="companyNames" />
  </view>
</template>

<script>
import companyInfo from "@/components/companyInfo.vue"
const app = getApp();
let Store = app.globalData.Store;
let Sign = app.globalData.Sign;
let Api = app.globalData.Api;
export default {
  components: { companyInfo },
  data() {
    return {
      // 表单数据
      formData: {
        token: 'fb112fd0765c4d9398eb4aba7e8bf5b2',
        userid: '0d77d5ed36ec4b56891717157162b7c3'
      },
      companyNames: '- ' + app.globalData.Appname + ' -'
    };
  }, // 生命周期函数--监听页面加载
  onLoad: function (options) {
    //跳转源页面
    Store.setItem('pay_source', options.pay_source);
  },
  methods: {
    submitForm() {
      // 没有对接登录，模拟登录
      Store.setItem('token', this.formData.token);
      Store.setItem('user_id', this.formData.userid);
      Store.setItem('ifLogged', 'true');
      app.globalData.readyToPay(this.formData.userid);
    },
    getOpenUserInfo: function (e) {
      my.getOpenUserInfo({
        success: (res) => {
          let userInfo = JSON.parse(res.response).response;
          console.info("userInfo", userInfo);
          // avatar: "https://tfs.alipayobjects.com/images/partner/T1Ia4pXnldXXXXXXXX"
          // city: ""
          // code: "10000"
          // countryCode: ""
          // gender: ""
          // msg: "Success"
          // nickName: "静而寒"
          // province: ""
          if (userInfo.code) {
            const signObject = {
              code: res.code
            };
            const signvalue = Sign.signutil(signObject);
            app.globalData
              .get(Api.getSession, {
                code: res.code,
                sign: signvalue
              })
              .then((re) => {
                Store.setItem('token', re.token);
                app.globalData.clickCheckLogin(e);
              })
              .catch((res) => {
                console.log('error:' + res);
              });
          }
        },
        fail: (err) => {
          console.log(err)
        }
      });
    },
    // 用户点击 注册
    getUserInfo(e) {
      console.log('e: ', e);
      if (e.detail.userInfo == undefined) {
        uni.showToast({
          title: '请点击允许授权后登录',
          icon: 'none'
        });
      } else {
        uni.login({
          success: (res) => {
            console.log('RES',res)
            if (res.code) {
              const signObject = {
                code: res.code
              };
              const signvalue = Sign.signutil(signObject);
              app.globalData
                .get(Api.getSession, {
                  code: res.code,
                  sign: signvalue
                })
                .then((re) => {
                  Store.setItem('token', re.token);
                  app.globalData.clickCheckLogin(e);
                })
                .catch((res) => {
                  console.log('error:' + res);
                });
            }
          }
        });
      }
    }
  }
};
</script>
<style>
@import "./sysLogin.css";
</style>
